const express = require('express');
const router = express.Router();


// 引入读取文件的中间件: readFileSync 同步读取文件
const { readFileSync } = require('fs');
// 找到绝对路径： resole 拼接路径
const { resolve } = require('path');

router.post('/getQuestion', function (req, res) {
    const order = req.body.order;
    // 路径是错误的
    const questionData = JSON.parse(readFileSync(resolve(__dirname, '../data/question.json'), 'utf-8'));
    const questionResult = questionData[order];

    if (questionResult) {
        const { id, question, items } = questionResult;
        res.send({
            errorCode: 0,
            msg: 'OK',
            data: {
                id,
                question,
                items
            }
        });
    } else {
        res.send({
            errorCode: 1,
            msg: 'NO-DATA',
            data: myResult
        });

        myResult = [];
    }

});

module.exports = router;